package com.atguigu.cloud.controller;

import com.aiguigu.cloud.resp.ResultData;
import com.atguigu.cloud.entities.Article;
import com.atguigu.cloud.service.ArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j //日志
public class ArticleController {
    @Resource
    private ArticleService articleService;

    @PostMapping(value = "/article/add")
    public ResultData<String> addArticle(@RequestBody Article article){
        int result = articleService.add(article);
        return ResultData.success("成功插入记录，返回值" + result);
    }

    @DeleteMapping(value = "/article/delete/{id}")
    public ResultData<Integer> deleteArticle(@PathVariable("id") Integer id){
        Integer result = articleService.delete(id);
        return ResultData.success(result);
    }

    @PutMapping(value = "/article/update")
    public ResultData<String> updateArticle(@RequestBody Article article){
        int result = articleService.update(article);
        return ResultData.success("修改成功，返回值" + result);
    }

    @GetMapping(value = "/article/get/{id}")
    public ResultData<Article> getById(@PathVariable("id") Integer id){
        if (id < 0){
            throw new RuntimeException("id不能为负数");
        }
        Article article = articleService.getById(id);
        return ResultData.success(article);
    }

    @GetMapping(value = "/article/get/all")
    public ResultData<List<Article>> getAll(){
        List<Article> articlesList = articleService.getAll();
        return ResultData.success(articlesList);
    }

    @Value("${server.port}")
    private String port;

    @GetMapping(value = "/article/get/info")
    public String getInfoByConsul(@Value("${atguigu.info}") String info){
        return "Article Consul Info: " + info + "\t" + "port: " + port;
    }

}
